package com.daocao.ds.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateTime {

    public final static long HALF_DAY = 12 * 3600 * 1000;
    public final static long DAY = 24 * 3600 * 1000;
    public final static long WEEKLY = 7 * DAY;

    private final static String FORMAT_DATE_TIME_MS = "yyyy-MM-dd HH:mm:ss.SSS";
    private final static String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    private final static String FORMAT_DATE_Miniute = "yyyy-MM-dd HH:mm";
    private final static String FORMAT_DATE = "yyyy-MM-dd";
    private final static String FORMAT_DATE_UTF8 = "yyyy年MM月dd日";

    private final static String FORMAT_MDSS = "MM月dd日 HH:mm";
    private final static String FORMAT_TIME = "HH:mm:ss";
    private final static String FORMAT_SHORTTIME = "HH:mm";
    private final static String FORMAT_MONTH = "yyyy-MM";
    private final static String FORMAT_MONTH_UTF8 = "yyyy年MM月";
    private final static String FORMAT_MONTH_DAY = "MM月dd日";
    private final static String FORMAT_DATE_TIME_NN = "yyyy-MM-dd\nHH:mm:ss";
    private static SimpleDateFormat _formatter = new SimpleDateFormat();

    static {
        _formatter.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    }

    /**
     * yyyy-MM-dd HH:mm:ss.SSS
     */
    public static String toDateTimeMs() {
        return format(FORMAT_DATE_TIME_MS, new Date());
    }

    public static String toDateTimeMs(Date date) {
        return format(FORMAT_DATE_TIME_MS, date);
    }

    public static String toDateTimeMs(long time) {
        return format(FORMAT_DATE_TIME_MS, new Date(time));
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static String toDateTime() {
        return format(FORMAT_DATE_TIME, new Date());
    }

    public static String toDateTime(Date date) {
        return format(FORMAT_DATE_TIME, date);
    }

    public static String toDateTime(long time) {
        return format(FORMAT_DATE_TIME, new Date(time));
    }

    /**
     * yyyy-MM-dd
     */
    public static String toDate() {
        return format(FORMAT_DATE, new Date());
    }

    public static String toDate(Date date) {
        return format(FORMAT_DATE, date);
    }

    public static String toDate(long time) {
        return format(FORMAT_DATE, new Date(time));
    }

    /**
     * HH:mm:ss
     */
    public static String toTime() {
        return format(FORMAT_TIME, new Date());
    }

    public static String toTime(Date date) {
        return format(FORMAT_TIME, date);
    }

    public static String toTime(long time) {
        return format(FORMAT_TIME, new Date(time));
    }

    /**
     * HH:mm
     */
    public static String toShortTime() {
        return format(FORMAT_SHORTTIME, new Date());
    }

    public static String toShortTime(Date date) {
        return format(FORMAT_SHORTTIME, date);
    }

    public static String toShortTime(long time) {
        return format(FORMAT_SHORTTIME, new Date(time));
    }
    public static String toMDSS(long time) {
        return format(FORMAT_MDSS, new Date(time));
    }

    /**
     * MM月dd日
     */
    public static String toMonthDay() {
        return format(FORMAT_MONTH_DAY, new Date());
    }

    public static String toMonthDay(Date date) {
        return format(FORMAT_MONTH_DAY, date);
    }

    public static String toMonthDay(long time) {
        return format(FORMAT_MONTH_DAY, new Date(time));
    }


    //yyyy-MM
    public static String toMonthTime(long time) {
        return format(FORMAT_MONTH, time);
    }

    //	yyyy年MM月
    public static String toMonthUTF8Time(long time) {
        return format(FORMAT_MONTH_UTF8, time);
    }

    //	yyyy年MM月NN日
    public static String toDayUTF8Time(long time) {
        return format(FORMAT_DATE_UTF8, new Date(time));
    }

    //	yyyy-MM-dd \n HH:mm:ss
    public static String toDateTimeNN(long time) {
        return format(FORMAT_DATE_TIME_NN, new Date(time));
    }

    public static String format(String format) {
        return format(format, new Date());
    }

    public static String format(String format, long time) {
        return format(format, new Date(time));
    }

    public static String format(String format, Date date) {
        synchronized (_formatter) {
            _formatter.applyPattern(format);
            return _formatter.format(date);
        }
    }
}
